package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Moneyflow;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class MoneyFlowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Moneyflow> moneyflowList;
    private Context mContext;


    public MoneyFlowAdapter(List<Moneyflow> list, Context context) {
        this.moneyflowList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (moneyflowList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.money_flow_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(moneyflowList.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (moneyflowList.size() > 0) {
            return moneyflowList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView numberTV;
        TextView payTypeTv;
        TextView moneyTV;
        ImageView arrowIV;

        ExpandView expandView;
        TextView payCompanyTV;
        TextView receiptCoompanyTV;
        TextView adduserTV;
        TextView oauthUserTV;
        TextView remarkTV;


        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            arrowIV = (ImageView) itemView.findViewById(R.id.arrow);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            payTypeTv = (TextView) itemView.findViewById(R.id.pay_type);
            moneyTV = (TextView) itemView.findViewById(R.id.money);
            payCompanyTV = (TextView) itemView.findViewById(R.id.pay_company);
            receiptCoompanyTV = (TextView) itemView.findViewById(R.id.receipt_company);
            adduserTV = (TextView) itemView.findViewById(R.id.add_user);
            oauthUserTV = (TextView) itemView.findViewById(R.id.oauth_user);
            remarkTV = (TextView) itemView.findViewById(R.id.remark);
        }

        public void BindViewData(Moneyflow moneyflow) {
            numberTV.setText(moneyflow.getNumber());
            if(moneyflow.getPaytype() == Moneyflow.PAY_TYPE_BANK){
                payTypeTv.setText("银行转账");
            }else {
                payTypeTv.setText("现金");
            }
            if(moneyflow.getStatus() == 1){
                adduserTV.setText("录入: "+moneyflow.getAddusername()+" "+ TimeUtils.parseDateTime(moneyflow.getAddtime()));
                oauthUserTV.setText("审核: " +moneyflow.getAudituname()+" "+ TimeUtils.parseDateTime(moneyflow.getAudittime()));
            }else {
                oauthUserTV.setText("未审核");
            }
            payCompanyTV.setText("付： "+moneyflow.getPaycompanyname());
            receiptCoompanyTV.setText("收： "+moneyflow.getReceivecompanyname());
            moneyTV.setText(moneyflow.getMoney()+"");
            String remark = moneyflow.getRemark();
            if (remark == null || remark.length() < 2) {
                remarkTV.setVisibility(View.GONE);
            } else {
                remarkTV.setText(remark);
            }
        }

        public void setViewListener(final int position) {
            arrowIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (expandView.isExpand()) {
                        expandView.collapse();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                    } else {
                        expandView.expand();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                    }
                }
            });
        }
    }
}
